import 'package:hongsheng/api/SPClassApiManager.dart';
import 'package:hongsheng/api/SPClassHttpCallBack.dart';
import 'package:hongsheng/app/SPClassApplicaion.dart';
import 'package:hongsheng/model/SPClassPointsProductEntity.dart';
import 'package:hongsheng/pages/user/signIn/SPClassPrizeDrawPage.dart';
import 'package:hongsheng/utils/SPClassCommonMethods.dart';
import 'package:hongsheng/utils/SPClassImageUtil.dart';
import 'package:hongsheng/utils/SPClassNavigatorUtils.dart';
import 'package:hongsheng/utils/SPClassStringUtils.dart';
import 'package:hongsheng/utils/colors.dart';
import 'package:hongsheng/widgets/SPClassSignInWidget.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'IntegralShop/SPClassInputInfo.dart';
import 'IntegralShop/SPClassIntegralShopPage.dart';
import 'SPClassTaskPage.dart';

import 'IntegralShop/IntegralProductDetail.dart';


///签到福利
class SPClassSignInPage extends StatefulWidget {
  const SPClassSignInPage({Key ?key}) : super(key: key);

  @override
  _SPClassSignInPageState createState() => _SPClassSignInPageState();
}

class _SPClassSignInPageState extends State<SPClassSignInPage> {
  List<SPClassProduct> phoneChargesList=[];
  List<SPClassProduct> recommendList=[];
  var myEventBus;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    myEventBus=SPClassApplicaion.spProEventBus.on<String>().listen((event) {
      if(event=="loginInfo"){
        setState(() {
        });
      }
    });

    // 话费
    SPClassApiManager.spFunGetInstance().spFunGetPointProduct<SPClassPointsProductEntity>(params: {"category_id":"2"},spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          phoneChargesList =result.spProProduct!;
          setState(() {

          });
        }
    ));
    // 精品
    SPClassApiManager.spFunGetInstance().spFunGetPointProduct<SPClassPointsProductEntity>(params: {'recommend':'1'},spProCallBack:SPClassHttpCallBack(
        spProOnSuccess: (result){
          recommendList =result.spProProduct!;
          setState(() {
          });
        }
    ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: <Widget>[
          Container(
            width: MediaQuery.of(context).size.width,
            height: MediaQuery.of(context).size.height,
            child: Column(
                children: <Widget>[
                  Image.asset(
                    SPClassImageUtil.spFunGetImagePath(
                        "ic_sign_bg"),
                    width: MediaQuery.of(context).size.width,
                  )
                ]
            ),
          ),
          Positioned(
            left: 0,
            right: 0,
            bottom: 0,
            top: 0,
            child:  Column(
              children: <Widget>[
                AppBar(
                  backgroundColor: Colors.transparent,
                  leading: FlatButton(
                    child: Icon(Icons.arrow_back_ios,size: width(20),color: Colors.white,),
                    onPressed: (){
                      Navigator.of(context).pop();
                    },
                  ),
                  centerTitle: true,
                  elevation: 0,
                  title: Text(
                    "签到福利",
                    style: TextStyle(fontSize: sp(18)),
                  ),
                ),
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      children: <Widget>[
                        userInfoWidget(),
                        SizedBox(height: width(23),),
                        SPClassSignInWidget(),
                        SizedBox(height: width(0),),
                        GestureDetector(
                          onTap: (){
                            SPClassNavigatorUtils.spFunPushRoute(
                                context,
                                SPClassPrizeDrawPage()
                            );
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            margin: EdgeInsets.symmetric(horizontal: width(10),vertical: width(10)),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(8),
                              boxShadow:[
                                BoxShadow(
                                  offset: Offset(0,0),
                                  color: Color(0xFF000000).withOpacity(0.16),
                                  blurRadius:width(6,),),
                              ],
                            ),
                            child: Image.asset(
                              SPClassImageUtil.spFunGetImagePath(
                                  "ic_zhuanpan"),
                              width: MediaQuery.of(context).size.width,
                            ),
                          ),
                        ),
                        GestureDetector(
                          onTap: (){
                            SPClassNavigatorUtils.spFunPushRoute(
                                context,
                                SPClassIntegralShopPage());
                          },
                          child: Container(
                            width: MediaQuery.of(context).size.width,
                            margin: EdgeInsets.symmetric(horizontal: width(10)),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(8),
                              boxShadow:[
                                BoxShadow(
                                  offset: Offset(0,0),
                                  color: Color(0xFF000000).withOpacity(0.16),
                                  blurRadius:width(6,),),
                              ],
                            ),
                            child: Image.asset(
                              SPClassImageUtil.spFunGetImagePath(
                                  "ic_duihuan"),
                              width: MediaQuery.of(context).size.width,
                            ),
                          ),
                        ),
                        SPClassTaskPage(),
                        phoneChargesWidget(),
                        recommendWidget(),
                        SizedBox(height: 50,)
                      ],
                    ),
                  ),
                )
              ],
            ),
          )


        ],
      ),
    );
  }

  Widget userInfoWidget(){
    return Container(
      child: Row(
        children: <Widget>[
          SizedBox(
            width: width(22),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.white, width: width(2)),
                borderRadius:
                BorderRadius.circular(width(27.5))),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(width(25.5)),
              child:
              (!SPClassApplicaion.spFunIsExistUserInfo() ||
                  SPClassApplicaion.spProUserInfo
                      !.spProAvatarUrl.isEmpty)
                  ? Image.asset(
                SPClassImageUtil.spFunGetImagePath(
                    "ic_default_avater"),
                width: width(51),
                height: width(51),
              )
                  : Image.network(
                SPClassApplicaion
                    .spProUserInfo!.spProAvatarUrl,
                fit: BoxFit.cover,
                width: width(51),
                height: width(51),
              ),
            ),
          ),
          SizedBox(
            width: width(8),
          ),
          Flexible(
            flex: 1,
            fit: FlexFit.tight,
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: SPClassApplicaion.spFunIsExistUserInfo()
                  ? <Widget>[
                Text(
                  SPClassApplicaion
                      .spProUserInfo!.spProNickName,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: sp(20)),
                ),
                Text(
                  "UID:" +
                      SPClassApplicaion.spProUserLoginInfo!.spProUserId.toString(),
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: sp(12)),
                ),
              ]
                  : <Widget>[
                Text(
                  "登录 / 注册",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                      fontSize: 18),
                ),
                Text(
                  "登录后可以获得更多奖励哦",
                  style: TextStyle(
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                      fontSize: 14),
                ),
              ],
            ),
          ),
          Container(
            child: Column(
              children: <Widget>[
                Text('${SPClassApplicaion.spProUserInfo!.spProPoints}',style: TextStyle(color: Colors.white,fontSize: sp(23)),),
                Text('我的积分',style: TextStyle(color: Colors.white,fontSize: sp(12)),),
              ],
            ),
          ),
          SizedBox(
            width: width(61),
          ),
        ],
      ),
    );
  }



//  话费充值
  Widget phoneChargesWidget(){
    return phoneChargesList.isEmpty?Container():Container(
      padding: EdgeInsets.only(left: width(12),right: width(12),top: width(14),),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              SizedBox(width: 4,),
              Text("话费充值卡",style:TextStyle(fontWeight: FontWeight.w600,fontSize: sp(15)),),
              SizedBox(width: 4,),
              Text("赚积分，兑话费",style: TextStyle(fontSize: sp(12),color: MyColors.grey_99,height: 2),),
            ],
          ),
          SizedBox(height: width(12),),
          Container(
            padding: EdgeInsets.only(left: width(17),right: width(17),bottom: width(17)),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow:[
                BoxShadow(
                  offset: Offset(0,0),
                  color: Color(0xFF000000).withOpacity(0.16),
                  blurRadius:width(6,),),
              ],
            ),
            child: GridView.builder(
                physics: NeverScrollableScrollPhysics(),
                shrinkWrap: true,
              itemCount: phoneChargesList.length,
               gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  crossAxisSpacing:width(9),
                  mainAxisSpacing: width(20),
                  childAspectRatio: width(150)/width(144),
               ),
               itemBuilder: (context,index){
                 SPClassProduct data = phoneChargesList[index];
                return GestureDetector(
                  onTap: (){
                    // SPClassNavigatorUtils.spFunPushRoute(context, IntegralProductDetail(data: data,));
                    SPClassNavigatorUtils.spFunPushRoute(context, SPClassInputInfo(data: data,));
                  },
                  child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Expanded(
                          child: Container(
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(4),
                              boxShadow:[
                                BoxShadow(
                                  offset: Offset(0,0),
                                  color: Color(0xFF000000).withOpacity(0.05),
                                  blurRadius:width(3,),),
                              ],
                            ),
                            child: Column(
                              children: <Widget>[
                                Expanded(
                                  child: Container(
                                    child: ClipRRect(
                                      borderRadius: BorderRadius.circular(4),
                                      child: Image.network(
                                        data.spProIcon!,
                                        fit: BoxFit.cover,
                                        height: double.maxFinite,
                                      ),
                                    ),
                                  ),
                                ),
                                // Container(
                                //   height: width(30),
                                //   color: Color(0xFFF2F2F2),
                                //   alignment: Alignment.center,
                                //   child: Text('7个工作日内到帐',style: TextStyle(fontSize: sp(10),color: MyColors.grey_99),),
                                // )
                              ],
                            ),
                            margin: EdgeInsets.all(1),
                          ),
                        ),
                        SizedBox(height: width(8),),
                        Text('${data.spProContent}',style: TextStyle(fontSize: sp(12),color: MyColors.grey_33),),
                        Text('${SPClassStringUtils.optimizePrice(double.parse(data.spProPrice!))}积分',style: TextStyle(fontSize: sp(12),color: MyColors.main1),),
                      ],
                    ),
                );
               },
            ),
          ),


        ],
      ),
    );
  }

//  精品推荐
  Widget recommendWidget(){
    return recommendList.isEmpty?Container():Container(
      padding: EdgeInsets.only(left: width(12),right: width(12),top: width(14),),
      child: Column(
        children: <Widget>[
          Row(
            children: <Widget>[
              SizedBox(width: 4,),
              Text("精品推荐",style: TextStyle(fontWeight: FontWeight.w600,fontSize: sp(15)),),
              SizedBox(width: 4,),
              Text("赚积分，兑好礼",style: TextStyle(fontSize: sp(12),color: MyColors.grey_99,height: 2),),
            ],
          ),
          SizedBox(height: width(12),),
          Container(
            padding: EdgeInsets.only(left: width(15),right: width(15),bottom: width(23),top: width(23)),
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(8),
              boxShadow:[
                BoxShadow(
                  offset: Offset(0,0),
                  color: Color(0xFF000000).withOpacity(0.16),
                  blurRadius:width(6,),),
              ],
            ),
            child: GridView.builder(
              padding: EdgeInsets.zero,
              physics: NeverScrollableScrollPhysics(),
              shrinkWrap: true,
              itemCount: recommendList.length,
              gridDelegate:SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: width(8),
                mainAxisSpacing: width(20),
                childAspectRatio: width(98)/width(161),
              ),
              itemBuilder: (context,index){
                SPClassProduct data = recommendList[index];
                return GestureDetector(
                  onTap: (){
                    // SPClassNavigatorUtils.spFunPushRoute(context, IntegralProductDetail(data: data,));
                    SPClassNavigatorUtils.spFunPushRoute(context, SPClassInputInfo(data: data,));

                  },
                  child: Container(
                    width: width(98),
                    height: width(161),
                    color: Colors.white,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        ClipRRect(
                          borderRadius: BorderRadius.circular(8),
                          child: Container(
                            width: width(98),
                            height: width(98),
                            margin: EdgeInsets.all(width(1)),
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(8),
                              boxShadow:[
                                BoxShadow(
                                  offset: Offset(0,0),
                                  color: Color(0xFF000000).withOpacity(0.1),
                                  blurRadius:width(1,),),
                              ],
                            ),
                            child: Image.network(
                              data.spProIcon!,
                              fit: BoxFit.cover,
                              width: width(98),
                            ),
                          ),
                        ),
                        Text('${data.spProProductName}',style: TextStyle(fontSize: sp(12),color: MyColors.grey_33),overflow: TextOverflow.ellipsis,),
                        Text('${SPClassStringUtils.optimizePrice(double.parse(data.spProPrice!))}积分',style: TextStyle(fontSize: sp(12),color: MyColors.main1),),
                      ],
                    ),
                  ),
                );
              },

            ),
          )

        ],
      ),
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    myEventBus.cancel();
  }





}
